//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    isDisable:true,
    bigFontsize:false,
    isNoyhmoney:false,
    noyhmoneyVal:'',
    payWay: 1,//1微信支付 2余额支付 3混合支付
    isOpenbalance:true
  },
  onLoad:function(){
    var that = this;
  },
  onShow:function(){
    var that = this;
    app.setNavtitle('优惠买单');
    that.setData({
      isFocus:true
    })
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      that.requestCode();
    }
    app.setCartnum();//更新购物车数量
  },
  requestCode: function (e) {
    var that = this;
    var data = {};
    data.map = 'applet_receivables_code';
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          app.globalData.codeImg = res.data.data.code;
          that.setData({
            codeImg: res.data.data.code,
            fullAmount: parseFloat(res.data.data.fullAmount),
            highAmount: parseFloat(res.data.data.highAmount),
            reducedAmount: parseFloat(res.data.data.reducedAmount),
            shopLogo: res.data.data.shopLogo,
            shopName: res.data.data.shopName,
            balance: parseFloat(res.data.data.balance),
          })
          
          if (that.data.balance > 0 && that.data.isOpenbalance){
            that.setData({
              payWay:2
            })
          }else{
            that.setData({
              payWay: 1
            })
          }
          that.checkNoyhmoney();
          app.globalData.shopNamelogo = {
            logo: res.data.data.shopLogo,
            name: res.data.data.shopName
          }
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
      }
    });
  },
  toggleNoyh:function(){
    var that = this;
    that.setData({
      isNoyhmoney: !that.data.isNoyhmoney
    })
    console.log(that.data.isNoyhmoney);
    if (!that.data.isNoyhmoney){
      that.setData({
        noyhmoneyVal: ''
      })
      that.checkNoyhmoney('noyhmoney');
    }
  },
  moneyChange:function(e){
    var that = this;
    var curVal = e.detail.value;
    var disable = true;
    var bigFontsize = that.data.bigFontsize;
    if (curVal.length>0){
      bigFontsize = true;
    }else{
      bigFontsize = false;
    }
    if (curVal>0){
      disable = false;
    }else{
      disable = true;
    }
    that.setData({
      moneyVal: curVal,
      isDisable: disable,
      bigFontsize: bigFontsize
    })
    that.checkNoyhmoney('money');
  },
  noyhmoneyChange: function (e) {
    var that = this;
    var curVal = e.detail.value;
    that.setData({
      noyhmoneyVal: curVal
    })
    that.checkNoyhmoney('noyhmoney');
  },
  payWayChange:function(e){
    console.log(e.detail.value);
    var that = this;
    var val = e.detail.value;
    var payWay = 1;
    if(val){
      payWay = 2;
    }
    this.setData({
      payWay: payWay,
      isOpenbalance:e.detail.value
    })
    console.log(that.data.payWay);
    that.checkNoyhmoney();
  },
  getMoneychangeType:function(e){
    var type = e.target.dataset.type;
    this.checkNoyhmoney(type);
  },
  checkNoyhmoney:function(type){
    var that = this;
    var fullAmount = parseFloat(that.data.fullAmount);
    var highAmount = parseFloat(that.data.highAmount);
    var reducedAmount = parseFloat(that.data.reducedAmount);
    var noyhmoneyVal = parseFloat(that.data.noyhmoneyVal) ? parseFloat(that.data.noyhmoneyVal):0;
    var moneyVal = parseFloat(that.data.moneyVal)>0 ? parseFloat(that.data.moneyVal):0;
    var payMoney = parseFloat(that.data.payMoney)>0 ? parseFloat(that.data.payMoney):0;
    var minusMoney = parseFloat(0);
    if (type == 'money') {
      if (noyhmoneyVal - moneyVal > 0){
        minusMoney = parseFloat(Math.floor((moneyVal - 0) / fullAmount) * reducedAmount);
        if (highAmount>0){
          minusMoney = parseFloat(minusMoney > highAmount ? highAmount : minusMoney);
        }
        payMoney = parseFloat(moneyVal - minusMoney);
        that.setData({
          noyhmoneyVal: "",
          isNoyhmoney: false
        })
      }else{
        payMoney = moneyVal;
      }
    } else if (type =='noyhmoney'){
      if (noyhmoneyVal - moneyVal > 0) {
        app.errorTip(that, "不参与优惠金额不能大于消费总金额", 2000);
        that.setData({
          noyhmoneyVal: ""
        })
        return;
      }
    }
    if (fullAmount>0){
      minusMoney = parseFloat(Math.floor((moneyVal - noyhmoneyVal) / fullAmount) * reducedAmount);
      if (highAmount > 0) {
        minusMoney = parseFloat(minusMoney > highAmount ? highAmount : minusMoney);
      }
      payMoney = parseFloat(moneyVal - minusMoney);
    }
    var payWay = that.data.payWay;
    var balance = parseFloat(that.data.balance);
    if (balance>0){
      if (payWay == 2 || payWay == 3) {
        console.log(payMoney + '---' + balance);
        payMoney = parseFloat(payMoney - balance);
        if (payMoney > 0 && payWay == 2) {
          that.setData({
            payWay: 3
          })
        }
      }else{
        console.log(payMoney + '---' + balance);
        payMoney = parseFloat(moneyVal - minusMoney);
      }
    }
    that.setData({
      minusMoney: minusMoney.toFixed(2),
      payMoney: payMoney.toFixed(2)
    })
  },
  payCodePage:function(){
    wx.navigateTo({
      url: '/subpages/paycode/paycode'
    })
  },
  orderPay: function (e) {
    var that = this;
    var data = {};
    data.map = 'applet_cash_pay';
    data.money = that.data.moneyVal;
    data.dropMoney = that.data.noyhmoneyVal;
    data.payWay = that.data.payWay;//1微信支付 2余额支付 3混合支付
    if (!data.money){
      return;
    }
    var realmoney = parseFloat(data.money);
    var minusMoney = parseFloat(that.data.minusMoney);
    var money = (parseFloat(realmoney - minusMoney)).toFixed(2);
    console.log(data);
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          if (data.payWay == 1 || data.payWay == 3){
            that.weChatPay(res.data.data,money);
          }else{
            that.setData({
              moneyVal: "",
              isDisable: true,
              noyhmoneyVal: '',
              minusMoney: '',
              payMoney: '',
              bigFontsize:false
            })
            wx.navigateTo({
              url: '/subpages0/successtip/successtip?money=' + money
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.em,
            showCancel: false
          });
        }
      },
      complete: function () {
        wx.hideToast();
      }
    });
  },
  weChatPay: function (params,money){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.requestPayment({
      'appId': params.appId,
      'timeStamp': params.timeStamp,
      'nonceStr': params.nonceStr,
      'package': params.package,
      'signType': params.signType,
      'paySign': params.paySign,
      'success': function (res) {
        console.log(res);
        setTimeout(function(){
          that.setData({
            moneyVal: "",
            isDisable: true,
            bigFontsize: false,
            noyhmoneyVal: '',
            minusMoney: '',
            payMoney: ''
          })
          console.log("支付成功");
          wx.navigateTo({
            url: '/pages/successtip/successtip?money=' + money
          })
          wx.hideLoading();
        },100)
      },
      'fail': function (res) {
        wx.hideLoading();
        wx.showModal({
          title: '',
          content: '支付失败',
          showCancel: false
        });
      }
    });
  }
})
